﻿using SmartPlant.Model;
using SmartPlant.Model.Biz;
using SmartPlant.Model.DbContext.Biz;
using SmartPlant.Services.Interfaces;
using SmartPlant.WinCore.Helper;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

namespace SmartPlant.Services
{
    public class AchievementService : BaseService, IAchievementService
    {
        #region 1
        public async Task<RESTfulResult<string>> SpecOutputAsync(string proId, string templatepath, string unitIDs, string insTypeIDs, bool exportFlag)
        {
            var body = new SpecOutputQueryObj()
            {
                UnitIDs = unitIDs,
                InsTypeIDs = insTypeIDs,
                ExportFlag = exportFlag
            };
            string uri = $"/api/Project/{proId}/Achievement-api/spec-output/{templatepath}";
            var result = await PostBodyAsync<string, SpecOutputQueryObj>(uri, body);
            return result;
        }
        public class SpecOutputQueryObj
        {
            /// <summary>
            /// 单元ID的集合
            /// </summary>
            public string UnitIDs { get; set; }

            /// <summary>
            /// 仪表类型ID的集合
            /// </summary>
            public string InsTypeIDs { get; set; }

            /// <summary>
            /// 分单元出图Y，全厂合并出图N
            /// </summary>
            public bool ExportFlag { get; set; }

            /// <summary>
            /// 仪表位号
            /// </summary>
            public string CmpntID { get; set; }
        }
        /// <summary>
        /// 索引表导出。
        /// AchievementController/Index
        /// </summary>
        /// <param name="proId"></param>
        /// <param name="unitids"></param>
        /// <param name="templatepath"></param>
        /// <param name="templatename"></param>
        /// <param name="exportFlag"></param>
        /// <returns></returns>
        public async Task<RESTfulResult<string>> IndexTableOutputAsync(string proId, string unitids, string templatepath, string templatename, bool exportFlag)
        {
            var body = new IndexOutputQueryObj() { UnitIDs = unitids, ExportFlag = exportFlag };
            string uri = $"/api/Project/{proId}/Achievement-api/Index/{templatepath}/{templatename}";
            var result = await PostBodyAsync<string, IndexOutputQueryObj>(uri, body);
            return result;
        }
        public class IndexOutputQueryObj
        {
            /// <summary>
            /// 单元ID的集合
            /// </summary>
            public string UnitIDs { get; set; }

            /// <summary>
            /// 分单元出图Y，全厂合并出图N
            /// </summary>
            public bool ExportFlag { get; set; }
        }
        public async Task<List<ComponentFunctionType>> GetSpecOutputPreviewAsync(string proId, string unitids)
        {
            string uri = $"/api/Project/{proId}/Achievement-api/spec-output-preview";
            var data = await PostBodyAsync<List<ComponentFunctionType>, string>(uri, unitids);
            //var data = await GetAsync<List<ComponentFunctionType>>(uri);
            return data.data;
        }
        public async Task<RESTfulResult<Stream>> ExportItemListBomAsync(string proId, GroupMode groupMode, Report report)
        {
            var postObj = report.CloneObject();
            postObj.ParametersObj.Clear();
            postObj.Name = report.Name + ".xlsx";
            postObj.ParametersObj.Add(new ReportParameter { Name = "GroupMode", Operator = "=", Value = ((byte)groupMode).ToString() });
            var url = $"/api/Project/{proId}/Achievement-api/output-bom";

            var result = await PostBodyStreamAsync(url, postObj);
            return result;
        }
        public async Task<List<string>> SpecOutputTemplatePath(string proId)
        {
            string url = $"/api/Project/{proId}/Achievement-api/spec-output-template-path";
            var res = await GetAsync<List<string>>(url);
            return res.data;
        }
        #endregion


        //public async Task<Report> UploadTemplateFileAsync(string proId, Report report)
        //{
        //    string uri = $"/api/Project/{proId}/Achievement-api/template-create";
        //    var rr = await PostBodyAsync<Report, Report>(uri, report);
        //    return rr.data;
        //}

        //public async Task<bool> DeleteTemplateFileAsync(string proId, int templateType, Report report)
        //{
        //    string url = $"/api/Project/{proId}/Achievement-api/template-delete/{templateType}";
        //    var rr = await PostBodyAsync<Report, Report>(url, report);
        //    return rr.succeeded;

        //}

        //public Task<Report> CreateReportAsync(string proId, Report report)
        //{
        //    throw new System.NotImplementedException();
        //}

        ///// <summary>
        ///// 查询report表里的记录（输出的）
        ///// </summary>
        ///// <param name="proId"></param>
        ///// <param name="templateType"></param>
        ///// <returns></returns>
        //public async Task<List<Report>> GetTemplatesAsync(string proId, string templateType)
        //{
        //    string uri = $"/api/Project/{proId}/Achievement-api/templates/{templateType}";
        //    var data = await GetAsync<List<Report>>(uri);
        //    return data;
        //}
        #region 模板背后的具体文件操作相关
        ///// <summary>
        ///// 模板上传（背后的文件）
        ///// AchievementController/TemplateUpload
        ///// </summary>
        ///// <param name="proId"></param>
        ///// <param name="templateType"></param>
        ///// <param name="templatepath"></param>
        ///// <param name="stream"></param>
        ///// <param name="fileName"></param>
        ///// <returns></returns>
        //public async Task<bool> UploadTemplateFileAsync(string proId, int templateType, string templatepath, Stream stream, string fileName)
        //{
        //    string uri = $"/api/Project/{proId}/Achievement-api/template-upload/{templateType}/{templatepath}";
        //    return await PostFileAsync(uri, stream, fileName);
        //}

        ///// <summary>
        ///// 模板文件下载
        ///// </summary>
        ///// <param name="proId"></param>
        ///// <param name="report">注意report对象的Name要带上具体的后缀(如.xlsx)</param>
        ///// <returns></returns>
        //public async Task<RESTfulResult<Stream>> DownloadTemplateFileAsync(string proId, Report report)
        //{
        //    string uri = $"/api/Project/{proId}/Achievement-api/template-download";
        //    var data = await PostBodyStreamAsync(uri, report);
        //    return data;
        //}

        #endregion
    }
}
